package com.wenlin.customerservice.domain;



import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/***
 *  Created with IntelliJ IDEA.
 *  User:  wenlin
 *  Date:  2018/2/9
 *  Time:  15:52
 *  Description:
 **/
public class HttpClient {
    /**
     * 发送HttpGet请求
     * @param url
     * @return
     */
    public static String sendGet(String url) {
        //1.获得一个httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //2.生成一个get请求
        HttpGet httpGet = new HttpGet(url);
        /*httpGet.setHeader("ytoappkey","mdmoutbase");
        httpGet.setHeader("timestamp","1520930514");
        httpGet.setHeader("sign","B263B78E9E3C3AF18B45286837CC1F7D");*/
        httpGet.setHeader("Content-Type","text/plain; charset=GBK");
        CloseableHttpResponse response = null;
        try {
            //3.执行get请求并返回结果
            response = httpclient.execute(httpGet);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String result = null;

        try {
            //4.处理结果，这里将结果返回为字符串
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity , "GBK");
            }
            System.out.println(result);
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    public static String sendPost(String url) {
        //1.获得一个httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //2.生成一个get请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("ytoappkey","mdminbase");
        String time  = String.valueOf(System.currentTimeMillis()/1000);
        httpPost.setHeader("timestamp",time);
        httpPost.setHeader("sign","63bf4b5d62cb237d8656430aa8aa3bff");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
       /* if(map!=null){
            for (Map.Entry<String, String> entry : map.entrySet()) {
                nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }*/
        CloseableHttpResponse response = null;
        try {
            //3.执行get请求并返回结果
            response = httpclient.execute(httpPost);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String result = null;
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(result);
        return result;
    }
    public static void main(String[] args) {
        //
       /* String url = "http://10.129.220.223:8002/mdminbase/customer/balance?customerCode=K99902244";
        HttpClient.sendPost(url);*/
       String url= "http://10.1.5.31:8110/?addr=";
       String param = "江苏省无锡市新吴区无锡高新技术产业开发区无锡市新区震泽路18号无锡国家软件园飞鱼座D幢";
        String params =URLEncoder.encode(param);
        HttpClient.sendGet(url+param);
    }
}
